﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Dts.Pipeline;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using Microsoft.SqlServer.Dts.Runtime;
using OSGeo.OGR;
using GDALSSIS.Core;

namespace GDALSSIS.OGR.Area
{
    [DtsPipelineComponent(
        DisplayName = "OGR Area",
        ComponentType = ComponentType.Transform
    )]
    
    public class Area : OGRTransformationBase
    {
        public override void ProvideComponentProperties()
        {
            base.ProvideComponentProperties();

            IDTSOutput100 defaultOutput;
            if (ComponentMetaData.OutputCollection[0].IsErrorOut)
            {
                defaultOutput = ComponentMetaData.OutputCollection[1];
            }
            else
            {
                defaultOutput = ComponentMetaData.OutputCollection[0];
            }

            //Add output column
            IDTSOutputColumn100 areaColumn = defaultOutput.OutputColumnCollection.New();
            areaColumn.Name = "Area";
            areaColumn.Description = "Area";
            areaColumn.SetDataTypeProperties(Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_R8, 0, 0, 0, 0);
        }

        public override DTSValidationStatus Validate()
        {
            DTSValidationStatus validation =  base.Validate();

            if (validation == DTSValidationStatus.VS_ISVALID)
            {
                bool cancel;

                //Validate input column
                if (ComponentMetaData.InputCollection[0].InputColumnCollection[0].DataType != DataType.DT_IMAGE)
                {
                    ComponentMetaData.FireError(0, ComponentMetaData.Name, "Invalid input column data type", string.Empty, 0, out cancel);
                    return DTSValidationStatus.VS_ISBROKEN;
                }
            }
            return validation;
        }

        public override void transform(ref PipelineBuffer buffer, int defaultOutputId, int inputColumnBufferIndex, int outputColumnBufferIndex)
        {
            //Get OGR Geometry from buffer
            byte[] geomBytes = new byte[buffer.GetBlobLength(inputColumnBufferIndex)];
            geomBytes = buffer.GetBlobData(inputColumnBufferIndex, 0, geomBytes.Length);
            Geometry geom = Geometry.CreateFromWkb(geomBytes);

            double area = geom.GetArea();

            buffer.SetDouble(outputColumnBufferIndex, area);
            buffer.DirectRow(defaultOutputId);
        }
    }
}
